home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!sun2!ua302aa
- From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: Dereferencing void**
- Date: 24 Jan 1996 21:33:55 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4e68k3$8e@sparcserver.lrz-muenchen.de>
- References: <ragnaroek1996Jan22.114915.9372@news2.compulink.com>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- dirdata@idirect.com (Phill Moran) writes:
-
- >If anyone has any sugestions about my current problem it woould be very
- >helpfull.
- >I need to read in, at run time, a variable number of field that are also of
- >varying types. My first thaought was:
-
- > void **field;
- > *field = new void * [NumberOfFields];
-
- This is a bad idea in general, because "field" is an uninitialized pointer,
- and dereferencing it can lead to unexpected results.
-
- Let us assume for the moment that you are talking about C, not about C++,
- so this is
-
- field = calloc(NumberOfFields, sizeof(void *));
-
- >The advantage with this is that I can access the fields by index. The problem
- >is how do I access them ? This snippet gives me errors
-
- > for (i=0;i<=NumberOfFields;i++)
- > ((void *) (field))[i] = new int;
-
- I am not sure what you are trying to do, but what is the problem
- with something as simple as
-
- for (i = 0; i < NumberOfFields; i++)
- field[i] = malloc(sizeof(int));
-
- >So whar I need is an alyternate suggestion about data structures or the
- >correct syntax.
-
- If you want to use C++, try to look up pointers and operator new in
- a book about C++. If you want to use C, first try to get rid of
- the C++ features in your code. Then try to read a book on C that
- explains the _basic_ concept behind pointers.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-